﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace TDBaseDataLibrary
{
    public class GameSprite : SpriteDef
    {
        protected float time;
        protected bool bstart;
        protected int nimgindex;
        public GameSprite(Texture2D texture, float frameTime, bool isLooping, int frameWidth, int frameHeight, int xCount, Vector2 center):
            base(texture,frameTime,isLooping,frameWidth,frameHeight,xCount,center)
        {
            time = 0;
            bstart = false;
        }
        public virtual void Start(GameTime gametime)
        {
            if (!bstart)
            {
                time = (float)gametime.TotalGameTime.TotalMilliseconds;
                bstart = true;
            }
        }
        public virtual void Update(GameTime gametime)
        {
        }
        public override void Draw(GraphicsDeviceManager graphics, GameTime gameTime, SpriteBatch spriteBatch, int img, Vector2 pos,float fAngle,Color color)
        {
            base.Draw(graphics, gameTime, spriteBatch, img, pos, fAngle, color);
        }
   }
    public class RoleSprite : SpriteDef
    {
        protected float time;
        protected bool bstart;
        protected int nimgindex;

        public static Texture2D liftbound;
        public static Texture2D liftshow;

        public RoleSprite(ContentManager content, string resource, float frameTime, bool isLooping, int frameWidth, int frameHeight, int xCount, Vector2 center) :
            base(content,resource, frameTime, isLooping, frameWidth, frameHeight, xCount, center)
        {
            time = 0;
            bstart = false;
            liftbound = content.Load<Texture2D>("liftbound");
            liftshow = content.Load<Texture2D>("liftshow");
        }

        public virtual void Start(GameTime gametime)
        {
            if (!bstart)
            {
                time = (float)gametime.TotalGameTime.TotalMilliseconds;
                bstart = true;
            }
        }
        public virtual void Update(GameTime gametime,int direct)
        {
            int ntime = (int)(gametime.TotalGameTime.TotalMilliseconds - time);
            int nindex = ntime / 200;
            nimgindex = nindex % 2 + 11 + direct * 13;
        }
        public void Draw(GraphicsDeviceManager graphics, GameTime gameTime, SpriteBatch spriteBatch, int img, Vector2 pos, float fAngle,int liftper,Color color)
        {
            base.Draw(graphics, gameTime, spriteBatch, img, pos, fAngle, color);
            Rectangle rtSpriteD = new Rectangle((int)pos.X, (int)pos.Y, 64, 6);
            Rectangle rtSpriteSrc = new Rectangle(0, 0, 64, 6);
            spriteBatch.Draw(liftbound, rtSpriteD, rtSpriteSrc, Color.White, fAngle, GetCenter(), SpriteEffects.None, 0);
            rtSpriteD = new Rectangle((int)pos.X, (int)pos.Y, 64 * liftper / 100, 6);
            rtSpriteSrc = new Rectangle(0, 0, 64 * liftper / 100, 6);
            spriteBatch.Draw(liftshow, rtSpriteD, rtSpriteSrc, Color.White, fAngle, GetCenter(), SpriteEffects.None, 0);
        }
    }

    public class EnemySprite : SpriteDef
    {
        protected float time;
        protected bool bstart;
        protected int nimgindex;

        public static Texture2D liftbound;
        public static Texture2D liftshow;

        public EnemySprite(ContentManager content, string resource, float frameTime, bool isLooping, int frameWidth, int frameHeight, int xCount, Vector2 center) :
            base(content,resource, frameTime, isLooping, frameWidth, frameHeight, xCount, center)
        {
            time = 0;
            bstart = false;
        }

        public EnemySprite(Texture2D texture,float frameTime, bool isLooping, int frameWidth, int frameHeight, int xCount, Vector2 center) :
            base(texture, frameTime, isLooping, frameWidth, frameHeight, xCount, center)
        {
            time = 0;
            bstart = false;
        }
        public virtual void Start(GameTime gametime)
        {
            if (!bstart)
            {
                time = (float)gametime.TotalGameTime.TotalMilliseconds;
                bstart = true;
            }
        }
        public virtual void Update(GameTime gametime)
        {
        }
        public void Draw(GraphicsDeviceManager graphics, GameTime gameTime, SpriteBatch spriteBatch, int img, Vector2 pos, float fAngle,int liftper,Color color)
        {
            base.Draw(graphics, gameTime, spriteBatch, img, pos, fAngle,color);
            Rectangle rtSpriteD = new Rectangle((int)pos.X, (int)pos.Y, 64, 6);
            Rectangle rtSpriteSrc = new Rectangle(0, 0, 64, 6);
            spriteBatch.Draw(liftbound, rtSpriteD, rtSpriteSrc, Color.White, fAngle, GetCenter(), SpriteEffects.None, 0);
            rtSpriteD = new Rectangle((int)pos.X, (int)pos.Y, 64 * liftper / 100, 6);
            rtSpriteSrc = new Rectangle(0, 0, 64 * liftper / 100, 6);
            spriteBatch.Draw(liftshow, rtSpriteD, rtSpriteSrc, Color.White, fAngle, GetCenter(), SpriteEffects.None, 0);
        }
    }
    class GameAmm : GameSprite
    { 
        public float Speed;
        public GameAmm(Texture2D texture, float frameTime, bool isLooping, int frameWidth, int frameHeight, int xCount, Vector2 center,float Speed)
            : base(texture, frameTime, isLooping, frameWidth, frameHeight, xCount, center)
        {
            this.Speed = Speed;
        }
        
        public void Update(GameTime gametime,TDCharacter desEnemy)
        {
            
        }
        public override void Draw(GraphicsDeviceManager graphics, GameTime gameTime, SpriteBatch spriteBatch, int img, Vector2 vPos, float fAngle, Color color)
        {
            base.Draw(graphics, gameTime, spriteBatch, img, vPos,fAngle,color);
        }
    }
    class ammFire
    {
        Vector2 vPos;
        float fAngle;
        float time;
        float lifttime;
        float alltime;
        float fAc;
        int nIndex;
        bool dead;
        GameAmm amm;
        public bool Dead
        {
            get { return dead; }
            set { dead = value; }
        }
        public ammFire(GameAmm amm,float fAngle,Vector2 vPos)
        {
            this.vPos = vPos;
            this.fAngle = fAngle;
            this.amm = amm;
            time = 0;
            alltime = 0;
            nIndex = 0;
            lifttime = 2000;
            fAc = 5;
            dead = false;
        }

        bool HitEnemy(Vector2 Enemypos, Vector2 Start, Vector2 End)
        {
            Vector2 vDStart = Start - Enemypos;
            Vector2 vDEnd = End - Enemypos;
            float f = Vector2.Dot(vDStart, vDEnd);
            if (f < 0)
                return true;
            return false;
        }
        public void Update(GameTime gametime, TDCharacter desEnemy, float fAngle)
        {
            this.fAngle = fAngle;
            time += (float)gametime.ElapsedGameTime.TotalMilliseconds;
            alltime += (float)gametime.ElapsedGameTime.TotalMilliseconds;
            if (time >= 40)
            {
                time -= 40;
                nIndex = (nIndex + 1) % 10;
            }
            if (alltime >= lifttime)
            {
                dead = true;
            }
            if (desEnemy != null && desEnemy.state == spritestate.liveing)
            {
                //bool bhit = HitEnemy(desEnemy.Pos, vPos, vEnd);
                //if (bhit)
                //{
                    float dc = desEnemy.GetAttr(enumattr.attr_dc);
                    float flift = desEnemy.GetStatus(enumstatus.status_lift);
                    float fLiftGain = (float)(Math.Max((fAc - dc), 1) * gametime.ElapsedGameTime.TotalMilliseconds / 1000);
                    desEnemy.SetStatus(enumstatus.status_lift, fLiftGain);
                //}
            }
            amm.Update(gametime, desEnemy);
        }
        public void Draw(GraphicsDeviceManager graphics, GameTime gameTime, SpriteBatch spriteBatch,int OffsetX,int OffsetY)
        {
            Vector2 vCurPos = new Vector2();
            vCurPos.X = vPos.X + OffsetX;
            vCurPos.Y = vPos.Y + OffsetY;
            amm.Draw(graphics, gameTime, spriteBatch, nIndex, vCurPos, fAngle,Color.White);
        }
    }
    class Arrow
    {
        Vector2 vPos;
        Vector2 vStartPos;
        public bool dead;
        GameAmm amm;
        float fAngle;
        float fXSpeed;
        float fYSpeed;
        int Ac;
        
        public Arrow(GameAmm amm,float fAngle,Vector2 vPos)
        {
            this.vPos = vPos;
            vStartPos = vPos;
            this.fAngle = fAngle;
            fXSpeed = -(float)(Math.Sin(fAngle) * amm.Speed);
            fYSpeed = (float)(Math.Cos(fAngle) * amm.Speed);
            this.amm = amm;
            dead = false;
            Ac = 1;
        }
        bool HitEnemy(Vector2 Enemypos,Vector2 Start, Vector2 End)
        {
            Vector2 vDStart = Start - Enemypos;
            Vector2 vDEnd = End - Enemypos;
            float f = Vector2.Dot(vDStart, vDEnd);
            if (f < 0)
                return true;
            return false;
        }
        public void Update(GameTime gametime, TDCharacter desEnemy)
        {
           
        
            float dx = (float)(gametime.ElapsedGameTime.TotalMilliseconds / 1000.0 * fXSpeed);
            float dy = (float)(gametime.ElapsedGameTime.TotalMilliseconds / 1000.0 * fYSpeed);
            Vector2 vEnd = new Vector2(vPos.X + dx,vPos.Y + dy);

            if (desEnemy != null && desEnemy.state == spritestate.liveing)
            {
                bool bhit = HitEnemy(desEnemy.m_pos, vPos, vEnd);
                if (bhit)
                {
                    dead = true;
                    desEnemy.AddStatus(enumstatus.status_lift,Ac);
                }
                else if (Vector2.DistanceSquared(vPos, vStartPos) > 40000)
                {
                    dead = true;

                }
            }
            vPos = vEnd;
            amm.Update(gametime, desEnemy);
        }
        public void Draw(GraphicsDeviceManager graphics, GameTime gameTime, SpriteBatch spriteBatch, int OffsetX, int OffsetY)
        {
            Vector2 vCurPos = new Vector2();
            vCurPos.X = vPos.X + OffsetX;
            vCurPos.Y = vPos.Y + OffsetY;
            amm.Draw(graphics, gameTime, spriteBatch, 0, vCurPos, fAngle,Color.White);
        }

    }
}
